package org.example.server.messagehandler.auth;

import cn.hutool.core.util.StrUtil;
import io.netty.channel.Channel;
import org.example.common.codec.Invocation;
import org.example.common.dispacher.MessageHandler;
import org.example.server.message.auth.AuthRequest;
import org.example.server.message.auth.AuthResponse;
import org.example.server.server.NettyChannelManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @title
 * @time 2025/6/16 17:32
 */
@Component
public class AuthRequestHandler implements MessageHandler<AuthRequest> {
    @Autowired
    private NettyChannelManager nettyChannelManager;
    @Override
    public void execute(Channel ch, AuthRequest message) {
        // 如果未传入 token
        if (StrUtil.isBlank(message.getAccessToken())) {
            AuthResponse response = new AuthResponse(500,"未认证");
            ch.writeAndFlush(new Invocation(response.TYPE,response));
            return;
        }

        // 认证成功,根据token获取 用户信息
        // ........暂时省略

        // 将channel和用户绑定

        // 先暂时直接使用token 作为用户标识
        nettyChannelManager.addUser(ch,message.getAccessToken());
        // 构造结果响应
        AuthResponse response = new AuthResponse(200,"认证成功");
        ch.writeAndFlush(new Invocation(response.TYPE,response));
    }

    @Override
    public String getType() {
        return AuthRequest.TYPE;
    }
}
